package com.cqjtu.store.favorite.service.impl;

import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.ServiceCode;
import com.cqjtu.store.common.ServiceException;
import com.cqjtu.store.favorite.mapper.FavoriteMapper;
import com.cqjtu.store.favorite.pojo.dao.FavoritePageDTO;
import com.cqjtu.store.favorite.pojo.entity.Favorite;
import com.cqjtu.store.favorite.service.FavoriteService;
import com.cqjtu.store.product.mapper.ProductMapper;
import com.cqjtu.store.product.pojo.entity.Product;
import com.cqjtu.store.product.pojo.vo.ProductVO;
import com.cqjtu.store.user.pojo.dto.UserLoginDTO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.cqjtu.store.common.Constants.COLLECT_PRODUCT_STATS_PREFIX;

/**
 * @ProjectName: cqjtu-store
 * @Titile: FavoriteServiceImpl
 * @Author: Lucky
 * @Description: 收藏的业务类
 */
@Service
@Transactional //事务
public class FavoriteServiceImpl implements FavoriteService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private ProductMapper productMapper;
    @Override
    public PageVO<List<ProductVO>> getFavoriteList(FavoritePageDTO favoritePageDTO,
                                                   UserLoginVO userLoginVO) {
        //取出用户编号
        Integer userId = userLoginVO.getId();
        favoritePageDTO.setUserId(userId);
        List<Product> productList = favoriteMapper.getFavoriteList(favoritePageDTO);
        if(productList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该用户没有收藏商品");
        }
        List<ProductVO> productVOList  =new ArrayList<>();
        productList.forEach(product -> {
            ProductVO productVO = new ProductVO();
            productVO.setId(product.getId());
            productVO.setTitle(product.getTitle());
            productVO.setPrice(product.getPrice());
            productVO.setImage(product.getImage());
            productVOList.add(productVO);
        });
        //获取这个用户的总收藏商品数据量
        int count = favoriteMapper.count(userLoginVO.getId());
        PageVO<List<ProductVO>> pageVO = new PageVO<>(favoritePageDTO.getPageIndex(),
                favoritePageDTO.getPageSize(), count,productVOList);
        return pageVO;
    }

    @Override
    public void addFavorite(Integer productId, UserLoginVO userLoginVO) {
        //判断商品编号是否合法
        if(productId ==null){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"商品编号参数不能为空");
        }
        Product product = productMapper.getById(productId);
        if(product == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"商品不存在");
        }
        //执行到这里说明商品是存在的
        Favorite favorite = favoriteMapper.getFavorite(productId, userLoginVO.getId());
        if(favorite !=null){
            //已经收藏
            throw new ServiceException(ServiceCode.ERR_EXISTS,"该商品已经被收藏");
        }
        favorite = new Favorite();
        favorite.setUserId(userLoginVO.getId());
        favorite.setProductId(productId);
        favorite.setCreatedUser(userLoginVO.getUsername());
        //添加收藏
        int result =  favoriteMapper.insert(favorite);
        if(result != 1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"收藏失败");
        }
        //商品收藏量加入
        String collectKey = COLLECT_PRODUCT_STATS_PREFIX+productId;
        stringRedisTemplate.opsForSet().add(collectKey, userLoginVO.getId() +"");
        System.out.println("添加收藏");
    }

    @Override
    public void cancelFavorite(Integer productId, UserLoginVO userLoginVO) {
        //判断商品编号是否合法
        if(productId ==null){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"商品编号参数不能为空");
        }
        int result = favoriteMapper.delete(productId, userLoginVO.getId());
        if(result == 0){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"收藏的商品不存在");
        }
        //商品收藏量加入
        String collectKey = COLLECT_PRODUCT_STATS_PREFIX+productId;
       Long i=  stringRedisTemplate.opsForSet().remove(collectKey,userLoginVO.getId()+"");
        System.out.println(i+"取消收藏");
    }
}
